<?php
class DangkiesController extends AppController {

	var $name = 'Dangkies';
    var $components = array('Common','Session','Auth','Email', 'RequestHandler','Redirect');
    var $helpers = array('Html', 'Js','Form','Ajax','Javascript');
    var $uses = array('Dangky','Yeucaucm','Yeucautd','Trinhdo','Chuyenmon','Noilamviec','Tinhtp');
	function index() 
    {
		$this->Dangky->recursive = 2;
		$this->set('dangkies', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid dangky', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('dangky', $this->Dangky->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Dangky->create();
			if ($this->Dangky->save($this->data)) {
				$this->Session->setFlash(__('The dangky has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The dangky could not be saved. Please, try again.', true));
			}
		}
		//$users = $this->Dangky->User->find('list');
		//$loaihosos = $this->Dangky->Loaihoso->find('list');
		//$linhvucs = $this->Dangky->Linhvuc->find('list');
		$this->set(compact('users', 'loaihosos', 'linhvucs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid dangky', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Dangky->save($this->data)) {
				$this->Session->setFlash(__('The dangky has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The dangky could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Dangky->read(null, $id);
		}
		$users = $this->Dangky->User->find('list');
		//$loaihosos = $this->Dangky->Loaihoso->find('list');
		//$linhvucs = $this->Dangky->Linhvuc->find('list');
		$this->set(compact('users', 'loaihosos', 'linhvucs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for dangky', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Dangky->delete($id)) {
			$this->Session->setFlash(__('Dangky deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Dangky was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    function dangtin(){
        
    if(!empty($this->data))
    {
        $record['Dangky']['soluong']  =$this->data['Dangky']['sl'];
        $record['Dangky']['kinhnghiem']  =$this->data['Dangky']['kn'];
        $record['Dangky']['hinhthuc']=$this->data['Dangky']['ht'];
        $record['Dangky']['user_id']  =1;
        $record['Dangky']['tencv']   =$this->data['Dangky']['tcv'];
        $record['Dangky']['mucluong']=$this->data['Dangky']['ml'];
        $record['Dangky']['ghi_chu']  =$this->data['Dangky']['gc'];
        $record['Dangky']['thoihan']  =$this->data['Dangky']['th'];
        $record['Dangky']['ngaydang'] =date('Y-m-d');
        if(!$this->Dangky->saveAll($record))
            $this->Session->setflash('sai');
        //lay bien dk
        $dk_id = $this->Dangky->id;
        //debug($this->data);
        //noi lam viec
        $this->Noilamviec->create();
        foreach($this->data['Dangky']['tinhtp'] as $value)
        {
            $record['Noilamviec']['dangkies_id']=$dk_id;
            $record['Noilamviec']['id_t']=$value;
            if(!$this->Noilamviec->saveAll($record))
                $this->Session->setflash('sai');
        }
        //chuyen mon
        $this->Yeucaucm->create();
        foreach($this->data['Dangky']['chuyenmon'] as $value)
        {
            $record['Yeucaucm']['chuyenmon_id']=$value;
            $record['Yeucaucm']['dangkies_id']=$dk_id;
            if(!$this->Yeucaucm->saveAll($record))
            $this->Session->setflash('sai');
        }
                 //trinh do
        $this->Yeucautd->create();
        foreach($this->data['Dangky']['trinhdo'] as $value)
        {
            $record['Yeucautd']['trinhdos_id']=$value;
            $record['Yeucautd']['dangkies_id']=$dk_id;
            if(!$this->Yeucautd->saveAll($record))
            $this->Session->setflash('sai');
        }
        $this->Redirect->flashSuccess('Đăng thông tin tuyển dụng thành công', array('action' =>'index'));
    }
     $trinhdos = $this->Trinhdo->find('list',array('fields'=>array('diengiaitd')));
     $chuyenmons = $this->Chuyenmon->find('list',array('fields'=>array('diengiaicm')));
     $tinhtps = $this->Tinhtp->find('list',array('fields'=>array('tenttp')));
     $this->set(compact('trinhdos','chuyenmons','tinhtps'));
     
    }
    
    
    function tintuyendung()
    {
        $dangkies= $this->Dangky->find('all', array('conditions' => array('Dangky.users_id'=>$cur_id,'Dangky.ngaydang DESC')));
        $this->set('dangkies',$dangkies);
    }
    function hosoungvien()
    {
        $dangkies= $this->Dangky->find('all', array('conditions' => array($group_id=>2)));
        $this->set('dangkies',$dangkies);
    }
    function search() 
    {
        
     $chuyenmons = $this->Chuyenmon->find('list',array('fields'=>array('diengiaicm')));
     $tinhtps = $this->Tinhtp->find('list',array('fields'=>array('tenttp')));
      
    $this->Dangky->bindModel
    (
     array(
          'hasOne' => array(
                    'Noilamviec',
                    'FilterTinhtp' => array
                        (
                            'className' => 'Tinhtp',
                            'foreignKey' => false,
                            'conditions' => array('FilterTinhtp.id = Noilamviec.id_t')
                        )
                      
                    )
                    
                    
                    
         )
    );
    
     $this->Dangky->bindModel
    (
     array(
          'hasOne' => array(
                    'Yeucaucm',
                    'FilterChuyenmon' => array
                        (
                            'className' => 'Chuyenmon',
                            'foreignKey' => false,
                            'conditions' => array('FilterChuyenmon.id = Yeucaucm.chuyenmon_id')
                        )
                      
                    )
                    
                    
                    
         )
    );
    $tin=$this->Dangky->find('all', array(
    'fields' => array('Dangky.*'),
    'conditions'=>array('FilterTinhtp.id'=> ($this->data['Dangky']['tinhtp_id']),'FilterChuyenmon.id'=> ($this->data['Dangky']['chuyenmon_id'])
    )));


    $this->set(compact('chuyenmons','tinhtps','tin'));;
        
           
    }
    
}
?>